package test;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.swing.JFrame;

public class ShowImage extends Panel {
	BufferedImage image;
	private static final int	EXTERNAL_BUFFER_SIZE = 128000;
	public ShowImage() {
		try {
			String imageName = "C:\\Users\\shilpa\\workspace\\twinkle\\src\\HelloWorld.png";
			File input = new File(imageName);
			image = ImageIO.read(input);
		} catch (IOException ie) {
			System.out.println("Error:" + ie.getMessage());
		}
		
	//	repaint();
	}
	
//	public void paintImage()
//	{
//		Graphics g = getGraphics();
//		Graphics2D g2d = (Graphics2D)g;
//		g2d.drawImage(image, 0, 0, null);
//	}
	

	public void paint(Graphics g) {
		g.drawImage(image, 0, 0, null);
	}
	
	public static String getAbsolutePath(String file) {
		if (new File(file).isAbsolute()) {
			return file;
		}
		return new File("").getAbsoluteFile() + "\\src\\" + file;
	}

	
	public void displayImages(String[] images)
	{
		for(String i: images)
		{
			try {
				File input = new File(getAbsolutePath(i));
				image = ImageIO.read(input);
			} catch (IOException ie) {
				System.out.println("Error:" + ie.getMessage());
			}
			
			repaint();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void playSound()
	{
		String	strFilename = "C:\\Users\\shilpa\\workspace\\twinkle\\src\\helloWorld.wav";// args[0];
		File	soundFile = new File(strFilename);
	
		AudioInputStream	audioInputStream = null;
		try
		{
			audioInputStream = AudioSystem.getAudioInputStream(soundFile);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			System.exit(1);
		}

		AudioFormat	audioFormat = audioInputStream.getFormat();

		SourceDataLine	line = null;
		DataLine.Info	info = new DataLine.Info(SourceDataLine.class,
												 audioFormat);
		try
		{
			line = (SourceDataLine) AudioSystem.getLine(info);
			line.open(audioFormat);
		}
		catch (LineUnavailableException e)
		{
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		line.start();

		int	nBytesRead = 0;
		byte[]	abData = new byte[EXTERNAL_BUFFER_SIZE];
		while (nBytesRead != -1)
		{
			try
			{
				nBytesRead = audioInputStream.read(abData, 0, abData.length);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			if (nBytesRead >= 0)
			{
				int	nBytesWritten = line.write(abData, 0, nBytesRead);
			}
		}

		line.drain();
		line.close();
	}

	static public void main(String args[]) throws Exception {
		JFrame frame = new JFrame("Display image");
		ShowImage panel = new ShowImage();
	//	panel.paintImage();
		frame.getContentPane().add(panel);
		frame.setSize(500, 500);
		frame.setVisible(true);
		panel.playSound();
		String[] s = {"1.jpg","2.jpg","3.jpg","4.jpg"};
		panel.displayImages(s);
	}
	

}